package com.demo.spring_demo.mapper;

import com.demo.spring_demo.pojo.User;
import com.demo.spring_demo.pojo.UserAndUserInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Mapper
@ResponseBody
public interface UserMapper {

    /**
     * 1、登录
     * @return
     */
    @Select("select * from user where username=#{username} and password=#{password}")
    User login(@RequestBody User user);
    //访问数据库


    /**
     * 2、注册/添加用户
     * @param user
     * @return
     */
    @Insert("insert into user (userid,username,password,nickname,registerdate,email) values (null,#{username},#{password},#{nickname},#{registerDate},#{email})")
    int addUser(UserAndUserInfo user);

    /**
     * 3、删除用户
     * @param id
     * @return
     */
    @Delete("delete from user where userid=#{id}")
    int deleteUser(int id);

    /**
     * 4、获取数据库最后一个用户的账号(用来实现账号自动生成)
     * @return
     */
    @Select("select username from user where userid=(select max(userid) from user)")
    String getUsername();


    /**
     * 5、多表连接查询个人信息
     */
    UserAndUserInfo getUserAndUserInfo(String username);

    /**
     * 6、个人账户的修改搭配个人信息使用
     * @param user
     * @return
     */
    @Update("update user set password=#{password},nickname=#{nickname},email=#{email} where username=#{username}")
    int updateUser(User user);

    /**
     * 7、分页查询
     *  1、查询到的总条数
     *  2、查询到的数据
     */
    int getCount(@Param("user") User user);
    List<User> getUserList(@Param("user") User user,@Param("index") int index,@Param("size") int size);


    /**
     * 8、通过邮箱更改密码
     * @param user
     * @return
     */
    @Update("update user set password=#{password} where email=#{email} and username=#{username}")
    int updatePassword(User user);


}
